package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.TaskRecordListRes.TaskRecord;
import com.hengxinguotong.zhihuichengjian.picturebrowser.view.TaskBigImgActivity;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/27.
 */

public class SubmitTaskAdapter extends BaseAdapter {
    private List<TaskRecord> taskRecords;
    private Context context;
    private AualityTestAddPicAdapter aualityTestAddPicAdapter;

    public SubmitTaskAdapter(Context context, List<TaskRecord> taskRecords) {
        this.context = context;
        this.taskRecords = taskRecords;
    }


    @Override
    public int getCount() {
        return taskRecords.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_submit_task, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final TaskRecord taskRecord = taskRecords.get(position);
        if ("1".equals(taskRecord.getStatus())) {
            holder.stateTv.setText("新增任务");
            holder.stateIv.setImageResource(R.mipmap.already_confirmed);
        } else if ("2".equals(taskRecord.getStatus())) {
            holder.stateTv.setText("受理任务");
            holder.stateIv.setImageResource(R.mipmap.already_confirmed);
        } else if ("3".equals(taskRecord.getStatus())) {
            holder.stateTv.setText("提交检查");
            holder.stateIv.setImageResource(R.mipmap.already_confirmed);
        } else if ("4".equals(taskRecord.getStatus())) {
            holder.stateTv.setText("检查不通过");
            holder.stateIv.setImageResource(R.mipmap.canceled);
        } else if ("5".equals(taskRecord.getStatus())) {
            holder.stateTv.setText("检查通过");
            holder.stateIv.setImageResource(R.mipmap.already_confirmed);
        }
        holder.data.setText(taskRecord.getCreateTime());
        holder.name.setText(taskRecord.getName());
        if(!Utils.isEmpty(taskRecord.getRemark())){
            holder.remarkTv.setText(taskRecord.getRemark());
        }
        aualityTestAddPicAdapter = new AualityTestAddPicAdapter(context, taskRecord.getPicList(), new AualityTestAddPicAdapter.OnRecyclerViewClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                TaskBigImgActivity.startActivity(context, (ArrayList<String>) taskRecord.getPicList(), position);
            }

            @Override
            public void onClick(View v) {

            }
        }, false);
        holder.picRv.setLayoutManager(getLinearLayoutManager());
        holder.picRv.setAdapter(aualityTestAddPicAdapter);
        return convertView;
    }


    private LinearLayoutManager getLinearLayoutManager() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        return linearLayoutManager;
    }

    static class ViewHolder {
        @BindView(R.id.state_iv)
        ImageView stateIv;
        @BindView(R.id.state_tv)
        HXTextView stateTv;
        @BindView(R.id.remark_tv)
        HXTextView remarkTv;
        @BindView(R.id.pic_rv)
        RecyclerView picRv;
        @BindView(R.id.name)
        HXTextView name;
        @BindView(R.id.data)
        HXTextView data;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
